package com.chuangjiang.wxpayx.wxpayx.util;

import org.jdom.JDOMException;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

public class WxPayUtil {

    /**
     *
     * @param body
     * @param total_fee
     * @return
     */
    public static Map<String, String> unifiedorder(String body,String total_fee) {
        SortedMap<Object,Object> parameters = new TreeMap<Object,Object>();
        parameters.put("appid", ConfigUtil.APPID);
        parameters.put("sub_mch_id",ConfigUtil.Sub_Mch_ID);
        parameters.put("mch_id", ConfigUtil.MCH_ID);
        parameters.put("nonce_str", PayCommonUtil.CreateNoncestr());
        parameters.put("body", body);
        parameters.put("out_trade_no", Util.cardNo());
        parameters.put("total_fee", "100");
        parameters.put("spbill_create_ip","113.57.246.11");
        parameters.put("notify_url", ConfigUtil.NOTIFY_URL);
        parameters.put("trade_type", "NATIVE");
        parameters.put("product_id", "222");//商品ID
        String sign = PayCommonUtil.createMD5Sign(parameters);
        parameters.put("sign", sign);
        String requestXML = PayCommonUtil.getRequestXml(parameters);

        String result =CommonUtil.httpsRequest(ConfigUtil.UNIFIED_ORDER_URL, "POST", requestXML);
        Map<String, String> map=new HashMap<String, String>();
        try {
            map = XMLUtil.doXMLParse(result);
            System.out.println("map:"+map);
            QRCodeUtil.encode(map.get("code_url"),"d:/MyWorkDoc/1.jpg", "d:/MyWorkDoc", true);
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }//解析微信返回的信息，以Map形式存储便于取值
        catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}
